package com.speechb;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.ArrayList;
import java.util.List;

/**
 * 代码生成器
 */
public class DBCodeGenerator {

    private static final String SRC_MAIN_RESOURCES_MAPPER = "/web/src/main/resources/mapper/";
    private static final String table_name = "tag";
    private static final String SRC_MAIN_JAVA = "/web/src/main/java/";
    // private static String  basePackage = "com.speechb.modules.system.admin";
   private static String  basePackage = "com.speechb";
    //  private static final String SUPER_CONTROLLER_CLASS = "com.speechb.controller.base.base";
    public static void main(String[] args) {
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();
        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + SRC_MAIN_JAVA);
        gc.setAuthor("llj");
        gc.setOpen(false);

        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/mblog?serverTimezone=CTT");
        // dsc.setSchemaName("public");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        //数据库用户
        dsc.setUsername("root");
        //数据库密码
        dsc.setPassword("123456");
        mpg.setDataSource(dsc);
        // 包配置
        PackageConfig pc = new PackageConfig();
        // 设置父包。在此目录下生成代码
        pc.setParent(basePackage);
        mpg.setPackageInfo(pc);

        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
            }
        };

        // 如果模板引擎是 freemarker
        String templatePath = "/templates/mapper.xml.ftl";
        // 如果模板引擎是 velocity
        // String templatePath = "/templates/mapper.xml.vm";

        // 自定义输出配置
        List<FileOutConfig> focList = new ArrayList<>();
        // 自定义配置会被优先输出
        focList.add(new FileOutConfig(templatePath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输出文件名
                return projectPath + SRC_MAIN_RESOURCES_MAPPER
                        + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            }
        });

        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);
        mpg.setTemplate(new TemplateConfig().setXml(null));


        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        //大变小
        strategy.setColumnNaming(NamingStrategy.nochange);
  //      strategy.setSuperEntityClass("com.speechb.mybatis_plus_learn.entity");
       strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        //继承基本类
    //   strategy.setSuperControllerClass(SUPER_CONTROLLER_CLASS);
 /*        strategy.setSuperServiceClass("com.speechb.mybatis_plus_learn.service");
        strategy.setSuperServiceImplClass("com.speechb.mybatis_plus_learn.service.impl");
        strategy.setSuperMapperClass("com.speechb.mybatis_plus_learn.mapper");*/
        strategy.setInclude(table_name);
        strategy.setSuperEntityColumns("id");
        strategy.setControllerMappingHyphenStyle(true);
        strategy.setTablePrefix(pc.getModuleName() + "_");
        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }

}